//@PCSTART@//
package com.bluetane.pc;
//@PCEND@//
/*//@ANDROIDSTART@//
package com.bluetane.android;
//@ANDROIDEND@//*/

/*//@ANDROIDSTART@//
import com.bluetane.android.BluetaneEvent.BluetaneEventType;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import java.util.UUID;
//@ANDROIDEND@//*/

import java.io.IOException;

//@PCSTART@//
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.LocalDevice;
import javax.microedition.io.Connector;
import javax.obex.SessionNotifier;
import com.bluetane.pc.BluetaneEvent.BluetaneEventType;
//@PCEND@//


public class AwaitConnectionThread extends Thread
{
    /*//@ANDROIDSTART@//
    private final BluetoothServerSocket serverSocket;
    //@ANDROIDEND@//*/
    //@PCSTART@//
    SessionNotifier serverConn=null;
    //@PCEND@//

    private BluetaneEventSource eventSource;

    /*//@ANDROIDSTART@//
    public AwaitConnectionThread(BluetoothAdapter localBluetooth, String serviceName, UUID uniqueID, BluetaneEventSource startEventSource)
    {
        BluetoothServerSocket tempSocket = null;
        eventSource = startEventSource;
        try
        {
                tempSocket = localBluetooth.listenUsingRfcommWithServiceRecord(serviceName, uniqueID);
        }
        catch (IOException e)
        {
                // Socket Error
        }
        serverSocket = tempSocket;
    }
    //@ANDROIDEND@//*/

    //@PCSTART@//
    public AwaitConnectionThread(LocalDevice localDev, String connectionURL, BluetaneEventSource startEventSource)
    {
        SessionNotifier tempConn = null;
        eventSource = startEventSource;
        try
        {
            localDev.setDiscoverable(DiscoveryAgent.GIAC);//DiscoveryAgent.LIAC to limit time discoverable.
            tempConn = (SessionNotifier) Connector.open(connectionURL);

        }
        catch (IOException e)
        {
                // Socket Error
        }
        serverConn = tempConn;
    }
    //@PCEND@//

    public void run()
    {
        /*//@ANDROIDSTART@//
        BluetoothSocket socket = null;
        while(true)
        {
                try
                {
                        socket = serverSocket.accept();
                }
                catch (IOException acceptException)
                {
                        break;
                }
                if (socket != null)
                {
                        Connection socketConnection = new BtConnection(socket, eventSource);
                        eventSource.fireEvent(new BluetaneEvent(this, BluetaneEventType.ConnectionEstablished, socketConnection));
                        try
                        {

                                serverSocket.close();
                        }
                        catch (IOException closeException)
                        {

                        }
                        break;
                }
        }
    //@ANDROIDEND@//*/
    }

    public void cancel()
    {

        /*//@ANDROIDSTART@//
        try
        {
                serverSocket.close();
        }
        catch (IOException e)
        {
        }
        //@ANDROIDEND@//*/
    }
}
